package com.babster631;

import com.babster631folder2.babster631modbase;

import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.EnumToolMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class thundersword extends ItemSword
{

	public thundersword(int par1, EnumToolMaterial par2EnumToolMaterial) {
		super(par1, par2EnumToolMaterial);
		this.setCreativeTab(CreativeTabs.tabCombat);
	
		// TODO Auto-generated constructor stub
	}
	///public void registerIcons("iDTechModBase:thundersword");
	 /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        /*
        if (!par3EntityPlayer.capabilities.isCreativeMode)
        {
            --par1ItemStack.stackSize;
        }
        */
    	thunderSwordInUse = true;
        par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));

        if (!par2World.isRemote)
        {
                //par3EntityPlayer.entityDropItem(par1ItemStack, par2)
        	Vec3 skeyPos = util.placeInFrontOfEntity(par3EntityPlayer, 5);
        	skeyPos.yCoord = par2World.getPrecipitationHeight((int)skeyPos.xCoord, (int)skeyPos.zCoord);
        	//skeyPos.yCoord += 35par2World.getPrecipitationHeight((int)skeyPos.xCoord, (int)skeyPos.zCoord);
        	EntityLightningBolt skeley = (EntityLightningBolt)new EntityLightningBolt(par2World,skeyPos.xCoord,skeyPos.yCoord,skeyPos.zCoord);
                
        	/*
                skeley.setPosition(par3EntityPlayer.posX, par3EntityPlayer.posY, par3EntityPlayer.posZ);
                skeley.rotationYaw = par3EntityPlayer.rotationYaw - 90;
                skeley.posX -= ((double)(MathHelper.cos(skeley.rotationYaw / 180.0F * (float)Math.PI) * 0.16F)) * 100;
            skeley.posZ -= ((double)(MathHelper.sin(skeley.rotationYaw / 180.0F * (float)Math.PI) * 0.16F)) * 100;
            */
                
            par2World.spawnEntityInWorld(skeley);
            par2World.addWeatherEffect(skeley);
            par1ItemStack.damageItem(1, par3EntityPlayer);
        }
        //This is used to override the lightning effect to make the player block
        return super.onItemRightClick( par1ItemStack,  par2World,  par3EntityPlayer);
    }
    
    public void registerIcons(IconRegister iconReg)
    {
        itemIcon = iconReg.registerIcon("babster631modbase:thundersword");
        
    }
    
    /**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
    public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5)
    {
    	
    	EntityPlayer player = (EntityPlayer)par3Entity;
    	if (player != null)
    	{
    		if (player.getItemInUse() == par1ItemStack)
    		{
    			if (!thunderSwordInUse || par1ItemStack.getItemUseAction() != EnumAction.block)
    				return;
    			nextLightingBolt--;
    			if (nextLightingBolt <= 0)
    			{
	    			if (!par2World.isRemote)
			        {
			                //par3EntityPlayer.entityDropItem(par1ItemStack, par2)
			        	Vec3 skeyPos = util.placeInFrontOfEntity(par3Entity, 6);
			        	skeyPos.yCoord = par2World.getPrecipitationHeight((int)skeyPos.xCoord, (int)skeyPos.zCoord);
			        	EntityLightningBolt skeley = (EntityLightningBolt)new EntityLightningBolt(par2World,skeyPos.xCoord,skeyPos.yCoord,skeyPos.zCoord);
			                
			        	/*
			                skeley.setPosition(par3EntityPlayer.posX, par3EntityPlayer.posY, par3EntityPlayer.posZ);
			                skeley.rotationYaw = par3EntityPlayer.rotationYaw - 90;
			                skeley.posX -= ((double)(MathHelper.cos(skeley.rotationYaw / 180.0F * (float)Math.PI) * 0.16F)) * 100;
			            skeley.posZ -= ((double)(MathHelper.sin(skeley.rotationYaw / 180.0F * (float)Math.PI) * 0.16F)) * 100;
			            */
			                
			            par2World.spawnEntityInWorld(skeley);
			            par2World.addWeatherEffect(skeley);

			            par1ItemStack.damageItem(1, player);
			        }
	    			nextLightingBolt = 100000;
    			}
    			 
    		}
    	}
    }
   /**
    * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount
    */
   public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
   {
	   thunderSwordInUse = false;
   }
    
    /**
     * Returns the damage against a given entity.
     */
    public int getDamageVsEntity(Entity par1Entity)
    {
        return 10;
    }
    
    private int nextLightingBolt = 1000;
    private boolean thunderSwordInUse = false;

}
